<template>
  <div>
    <div style="margin: 16px;">
      <van-row>
        <van-col span=5> <van-button round
                      block
                      type="primary"
                      @click="goBack">
            返回
          </van-button></van-col>
        <van-col span=1> </van-col>

        <van-col span=8>
          <van-button round
                      block
                      type="primary"
                      @click="querymain">
            查询
          </van-button>

        </van-col>
      </van-row>
      <br />
      <van-col span=11>
        <van-button round
                    block
                    type="primary"
                    @click="createPdH">
          按数量新增盘点单
        </van-button>
      </van-col>

      <van-col span=1> </van-col>
      <van-col span=11>
        <van-button round
                    block
                    type="primary"
                    @click="createPdHByMakeno">
          按批号新增盘点单
        </van-button>

      </van-col>
      <van-row>

      </van-row>

    </div>
    <vue-good-table :columns="columns"
                    :rows="rows"
                    @on-row-click="onRowClick" />
  </div>
</template>
    
    <script>
import 'vue-good-table/dist/vue-good-table.css'
import { VueGoodTable } from 'vue-good-table';
import http from '../../api/http.js'

// add to component


export default {
  components: {
    VueGoodTable,
  },

  name: 'my-component',
  data () {
    return {
      addForm: {
        busno: "",
        orgname: "",
        createEmpname: "",
        type: ""
      },
      columns: [
        {
          label: '盘点单号',
          field: 'pdId',
        },
        {
          label: '门店海典编号',
          field: 'busno',
        },
        {
          label: '门店名称',
          field: 'orgname',
        },
        {
          label: '盘点创建时间',
          field: 'createDatetime',
        },
        {
          label: '创建人',
          field: 'createEmpname',
        },
        {
          label: '盘点类型',
          field: 'type',
        },
      ],
      rows: [
      ],
    };
  },
  mounted () {

    // console.log(this.$store.getters.getGlobalBusno)
    console.log(this.$store.getters.getGlobalOrgname)
    // const jsonData = [{ pdId: 1, busno: 101, orgname: "江门东湖中心店", createDatetime: "2024-05-10 09:39:39", createEmpname: "的" }]
    // this.rows = jsonData;

    http.fetchGet('/pd/qureyByBusno?busno=' + this.$store.getters.getGlobalBusno).then((data) => {
      if (data.data.msg == 'success') {
        //this.$toast(data.data.msg)
        // const jsonData = '{"pdId":1,"pdName":null,"busno":101,"orgname":"江门东湖中心店","createDatetime":"2024-05-10 09:39:39","createEmpname":"的","notes":null}'
        this.rows = data.data.data;
      }
    }).catch(err => {
      console.log(err)
    }
    )
    //alert(this.$route.params.busno); // 输出传递过来的参数
  },
  methods: {
    onRowClick (rows) {
      // console.info(rows.row.age);
      http.fetchPost('/pd/qureyPddCount?pdHid=' + rows.row.pdId + '&type=' + rows.row.type).then((data) => {
        console.info(data.data.data)
        if (data.data.msg != 'success') {
          this.$toast(data.data.msg)
        } else {
          if (data.data.data == 0) {
            this.$toast('还没生成盘点快照！请稍等')
            return false
          } else {
            if ('按数量' == rows.row.type) {
              this.$router.push({ name: 'pddetail', params: { id: rows.row.pdId } });
            } else if ('按批号' == rows.row.type) {
              this.$router.push({ name: 'pddetailmakeno', params: { id: rows.row.pdId } });
            }

          }
        }


      }).catch(err => {
        console.log(err)
      }
      )


    },
    goBack () {
      this.$router.push('/')
    },
    querymain () {
      http.fetchGet('/pd/qureyByBusno?busno=' + this.$store.getters.getGlobalBusno).then((data) => {
        if (data.data.msg == 'success') {
          //this.$toast(data.data.msg)
          // const jsonData = '{"pdId":1,"pdName":null,"busno":101,"orgname":"江门东湖中心店","createDatetime":"2024-05-10 09:39:39","createEmpname":"的","notes":null}'
          this.rows = data.data.data;
        }
      }).catch(err => {
        console.log(err)
      }
      )
    },
    createPdH () {
      this.$dialog.confirm({
        title: '标题',
        message:
          '是否确定生成按数量的盘点单？',
      })
        .then(() => {
          this.addForm.orgname = this.$store.getters.getGlobalOrgname
          this.addForm.busno = this.$store.getters.getGlobalBusno
          this.addForm.createEmpname = this.$store.getters.getGlobalEmpname
          this.addForm.type = 'Q'
          console.info(this.addForm.createEmpname)
          http.fetchPost('/pd/pdhCreate', this.addForm).then((data) => {

            if (data.data.msg != 'success') {
              this.$toast(data.data.msg)
            } else {
              this.$toast('生成成功！请等一分钟再进入盘点单')
              http.fetchGet('/pd/qureyByBusno?busno=' + this.$store.getters.getGlobalBusno).then((data) => {
                if (data.data.msg == 'success') {
                  //this.$toast(data.data.msg)
                  // const jsonData = '{"pdId":1,"pdName":null,"busno":101,"orgname":"江门东湖中心店","createDatetime":"2024-05-10 09:39:39","createEmpname":"的","notes":null}'
                  this.rows = data.data.data;
                }
              }).catch(err => {
                console.log(err)
              }
              )
            }


          }).catch(err => {
            console.log(err)
          }
          )
        })
        .catch(() => {

        });
    },
    createPdHByMakeno () {
      this.$dialog.confirm({
        title: '标题',
        message:
          '是否确定生成按批号的盘点单？',
      })
        .then(() => {
          this.addForm.orgname = this.$store.getters.getGlobalOrgname
          this.addForm.busno = this.$store.getters.getGlobalBusno
          this.addForm.createEmpname = this.$store.getters.getGlobalEmpname
          this.addForm.type = 'M'
          console.info(this.addForm.createEmpname)
          http.fetchPost('/pd/pdhCreate', this.addForm).then((data) => {

            if (data.data.msg != 'success') {
              this.$toast(data.data.msg)
            } else {
              this.$toast('生成成功！请等三分钟再进入盘点单')
              http.fetchGet('/pd/qureyByBusno?busno=' + this.$store.getters.getGlobalBusno).then((data) => {
                if (data.data.msg == 'success') {
                  //this.$toast(data.data.msg)
                  // const jsonData = '{"pdId":1,"pdName":null,"busno":101,"orgname":"江门东湖中心店","createDatetime":"2024-05-10 09:39:39","createEmpname":"的","notes":null}'
                  this.rows = data.data.data;
                }
              }).catch(err => {
                console.log(err)
              }
              )
            }


          }).catch(err => {
            console.log(err)
          }
          )
        })
        .catch(() => {

        });
    },
  }
};
    </script>
    
    <style>
</style>